package cz.cuni.amis.episodic.bayes.utils;

import java.awt.Point;

/**
 *
 * @author ik
 */
public class Utils {

    public static double[][] computeKL(NodeSnapshot[] p, NodeSnapshot[] q) {
        // compute KL
        double[][] kl = new double[p.length][];
        for (int i = 0; i < kl.length; i++) {
            kl[i] = p[i].computeKLDivergenceTo(q[i]);
        }
        return kl;
    }

    public static double max(double[][] data) {
        double max = Double.MIN_VALUE;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (max < data[i][j] && !Double.isNaN(data[i][j])) {
                    max = data[i][j];
                }
            }
        }
        return max;
    }

    public static double min(double[][] data) {
        double min = Double.MAX_VALUE;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (min > data[i][j] && !Double.isNaN(data[i][j])) {
                    min = data[i][j];
                }
            }
        }
        return min;
    }

    public static Object[] maxIndex(double[][] data) {
        Point p = new Point();
        double max = Double.MIN_VALUE;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (max < data[i][j]) {
                    max = data[i][j];
                    p.setLocation(i, j);
                }
            }
        }
        return new Object[]{p, max};
    }

    public static double[] maxToOneVector(double[] vec) {
        double[] vecRet = new double[vec.length];
        vecRet[maxIndex(vec)] = 1;
        return vecRet;
    }
    
    public static int maxIndex(double[] vec) {
        int maxIx = -1;
        double max = Double.MIN_VALUE;
        for (int i = 0; i < vec.length; i++) {
            if (max < vec[i]) {
                max = vec[i];
                maxIx = i;
            }
        }
        return maxIx;
    }
    
    
    public static double[] createOneVect(int ix, int length) {
        double[] vect = new double[length];
                vect[ix] = 1;
                return vect;
    }
}
